package com.ceba.test.task.subtask;

import com.ceba.cebautils.DateUtil;
import com.ceba.test.base.config.LogicPathUtil;
import com.ceba.test.modules.storage.logicService.StorageFileCacheManage;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.ceba.base.utils.IDSFileUtils;
import com.ceba.base.helper.LoggerHelper;
import com.ceba.test.base.configs.CebaConfig;
import com.ceba.test.base.configs.CebaPathUtil;
import com.ceba.test.base.enums.LogOpTypeEnum;
import com.ceba.test.base.enums.LogTypeEnum;
import com.ceba.test.modules.log.logicService.LogLogicService;
import com.ceba.test.modules.statistic.helper.CollectSignHelper;
import com.ceba.test.modules.statistic.logicService.StatisticLogicService;

import configs.context.SpringBeans;

public class CollectSignTimeTask implements Job{

	static Logger logger = LoggerFactory.getLogger(CollectSignTimeTask.class);
	private static LogLogicService logLogicService = SpringBeans.getLogLogicService("logLogicService");
	private StatisticLogicService statisticLogicService = SpringBeans.getStatisticLogicService("statisticLogicService");
	
	private String signCountFilePath = LogicPathUtil.getPhysicalS_LogsPath(StorageFileCacheManage.getSystemUseAddressId())+CebaPathUtil.getSignResordPathName();
	@Override
	public void execute(JobExecutionContext arg0) {
		try {
			String lastday = DateUtil.getDayTime(DateUtil.YYYY_MM_DD_,-1);
			String today = DateUtil.getNowTime(DateUtil.YYYY_MM_DD_HH_MM_SS);
			logger.info("凌晨{}统计{}签名份数开始-----",today,lastday);
			logLogicService.addLog(LogTypeEnum.SIGN_LOGTYPE.getType(),
					LogOpTypeEnum.TASKOPEN_OPYPE.getType(),"凌晨"+today +"统计"+lastday+"签名份数开始-----", "","");
			/**
			 * 对上年的年度签名份数清零
			 * 1、获取前一天的签名份数
			 * 2、签名统计写入sign.cb，前一天签名份数，以及所有签名分数，今年签名分数
			 * 3、记入统计表，按天统计，collectsign_report_attach
			 * 4、计入cb_log操作日志表
			 * 5、计入logger
			 * 
			 */
			String lastYear = lastday.substring(0, 4);
			String tYear = today.substring(0, 4);
			if(!tYear.equals(lastYear)){
				CebaConfig.set("YEAR_SIGNCOUNT_REPORT",  CebaConfig.getEncode("0"));
				CebaConfig.set("YEAR_SIGNCOUNT_ATTACH", CebaConfig.getEncode("0"));
			}
			long allSC_R = statisticLogicService.getAllReportSign();
			long YSC_R =statisticLogicService.getYearReportSign();
			long allSC_A =  statisticLogicService.getAllAttachSign();
			long YSC_A = statisticLogicService.getYearAttachSign();
			
			long lastdayR = statisticLogicService.getLastDayReportSign();
			long lastdayA = statisticLogicService.getLastDayAttachSign();
			long lastdayCount = lastdayR + lastdayA;
			
			long Other_A = logLogicService.getAllNoSignFileCount();
			
			StringBuffer logS = new StringBuffer();
			logS.append(today+"记录:  "+lastday+"报告签名"+lastdayR + "份,附件签名"+lastdayA+"份;")
				.append("今年报告签名"+YSC_R+"份,附件签名"+YSC_A+"份;")
				.append("全部报告签名"+allSC_R+"份,附件签名"+allSC_A+"份;")
				.append("其他文件"+Other_A+"份;");
			IDSFileUtils.writeFile(logS.toString().getBytes(), signCountFilePath, "utf-8");
			
			statisticLogicService.add(lastdayCount,DateUtil.getDayTime(DateUtil.YYYY_MM_DD_,-1));
			
			logLogicService.addLog(LogTypeEnum.SIGN_LOGTYPE.getType(), 
					LogOpTypeEnum.CREATE_OPYPE.getType(), logS.toString(), "","");
			logger.info(logS.toString());
			
			CollectSignHelper.backupSql();
			logLogicService.addLog(LogTypeEnum.SIGN_LOGTYPE.getType(), 
					LogOpTypeEnum.CREATE_OPYPE.getType(), "凌晨备份数据库", "","");
			
			String todaye = DateUtil.getNowTime(DateUtil.YYYY_MM_DD_HH_MM_SS);
			logger.info("凌晨{}统计签名份数结束-----",todaye);
			logLogicService.addLog(LogTypeEnum.SIGN_LOGTYPE.getType(), 
					LogOpTypeEnum.TASKCLOSE_OPYPE.getType(), "凌晨"+todaye+"统计签名份数结束-----", "","");
		} catch (Exception e) {
			LoggerHelper.error(logger, e,"凌晨统计错误");
		}
	}

	
	public static void main(String[]args){
		String lastday = DateUtil.getDayTime(DateUtil.YYYY_MM_DD_,-1);
		String today = DateUtil.getNowTime(DateUtil.YYYY_MM_DD_HH_MM_SS);
		String lastYear = lastday.substring(0, 4);
		String tYear = today.substring(0, 4);
		System.out.println(lastYear +" " + tYear);
	}
}
